/*
*资产配置模拟-->配置方案构建model
*configSchemeStructure.js
*/

import {routerRedux} from 'dva/router';
import api from '../api';

export default {
	namespace : 'configSconst',
	state : {
		strategyName:'',//方案名称
		strategyId:'',//方案id
		strategyDescription:'',//方案描述
		assets:[],//方案指标
		isShowModel:false,//是否展示配置模拟
		LargeAssetData:[],//资产名称
		detailData:[],//数据详情
		AssetName:'',//	大类资产
		AssetIndex:'',//资产指数（基金）
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/assetAllocation/assetSimulation') {
					dispatch({type: 'whetherRequest',payload:query,pathname})
				}
			})
		}
	},
	effects : {
		* whetherRequest({payload,pathname}, { put}) {
			console.log('whetherRequest>',payload,pathname)
			yield put({
				type:'getConfigureScheme',
			});
			yield put({
				type:'getLargeAsset',
			});
			yield put({
				type:'getPdtList',
				payload:{
					productType:'JIZHUN'
				}
			});
			
		},
		* getPdtList({payload},{call,put}){
			const response = yield call(api.construction.getPdtList, payload);
			console.log('getPdtList>>>',response)
			yield put({
        type: 'getPdtListSuccess',
        payload:response ,
      });
		},
		* getConfigureScheme({payload}, {call, put}){
			const response = yield call(api.construction.getConfigureScheme, payload);
			yield put({
        type: 'getConfigureSchemeSuccess',
        payload:response ,
      });
		},
		*addConfigureScheme({payload},{call,put}){
			const response = yield call(api.construction.addConfigureScheme, payload);
      yield put({
      	type: 'getConfigureScheme',
      })
		},
		* getOneConfigureScheme({payload},{call,put}){
			const response = yield call(api.construction.getOneConfigureScheme, payload);
			yield put({
        type: 'getOneConfigureSchemeSuccess',
        payload:response ,
      });
		},
		*updateScheme({payload},{call,put,select}){
			console.log('updateScheme>>',payload)
			const state=yield select(state => state.configSconst)
			const strategyId = state.strategyId
			const response = yield call(api.construction.updateScheme,strategyId,payload );
			yield put({
      	type: 'getConfigureScheme',
      })
		},
		* getLargeAsset({payload},{call,put,select}){
			const response = yield call(api.construction.getLargeAsset, payload);
			// console.log('getLargeAsset>>>',response)
			// const state=yield select(state => state.configSconst);
			// state.LargeAssetData = response.data;
			// console.log('state>>>>>>>>',state)
			yield put({
        type: 'getLargeAssetSuccess',
        payload:response ,
      });
		},
		* deleteScheme({payload},{call,put}){
			const strategyId = payload.strategyId
			const response = yield call(api.construction.deleteScheme,strategyId );
			yield put({
      	type: 'getConfigureScheme',
      })
		},
		* changeState({payload},{put}){
			yield put({
				type: 'setState',
				payload
			})
		},
		* targetSet({payload},{put,select}){
			console.log('targetSet>',payload)
			const state=yield select(state => state.configSconst)
			const targetObj = state.assets
			
			targetObj.map((item)=>{
				item.assetId = payload.AssetIndex;
				item.assetCode= payload.AssetName
			})
			console.log('targetSet>targetObj',targetObj)
		}
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		clearState(state) {
			return {
				
			}
		},
		getPdtListSuccess(state,{payload}){
			const data = payload.data;
			console.log('getPdtListSuccess>',payload,data)
			data.map((item)=>{
				delete item.bondRate
				delete item.description
				delete item.effectiveDate
				delete item.futuresRate
				delete item.isIncludeCash
				delete item.origProductType
				delete item.productCode
				delete item.productType
				delete item.sharesRate
			})

			console.log('getPdtListSuccess>>',data)
			return {
        ...state,
        pdtList:data
      }
			
		},
		getConfigureSchemeSuccess(state, {payload}){
			const data = payload.data;
			console.log('getConfigureSchemeSuccess>',payload,data)
			return {
        ...state,
        tableData:data//配置方案构建表格
      }
		},
		getOneConfigureSchemeSuccess(state,{payload}){
			const data = payload.data;
			console.log('getOneConfigureSchemeSuccess>',payload,data)
			return {
        ...state,
        detailData:data//配置方案构建表格
      }
		},
		getLargeAssetSuccess(state,{payload}){
			const data = payload.data;
			console.log('getLargeAssetSuccess>',payload,data)
			return {
        ...state,
        LargeAssetData:data//大类资产指标
      }
		},
	}
};
